import java.lang.reflect.Field;

public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
        //获取class字节码文件对象
        Class c = Class.forName("Student");

        //获取所有的成员变量
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        //获取单个的成员变量
        Field name = c.getDeclaredField("name");
        System.out.println(name);

        //获取权限修饰符
        int modifiers = name.getModifiers();
        System.out.println(modifiers);

        //获取成员变量的名字
        String n = name.getName();
        System.out.println(n);

        //获取成员变量的数据类型
        Class<?> type = name.getType();
        System.out.println(type);

        //获取成员变量记录的值
        Student student=new Student(11,"zhangsan");
        name.setAccessible(true);
        String value = (String) name.get(student);
        System.out.println(value);

        //修改对象里面记录的值
        name.set(student,"lisi");
        System.out.println(student);
    }
}
